package com.hlk.user.service.impl;

import com.hlk.enums.ResponseStatusEnum;
import com.hlk.enums.Sex;
import com.hlk.enums.UserStatusEnum;
import com.hlk.exception.HuaException;
import com.hlk.pojo.AppUser;
import com.hlk.pojo.bo.UpdateUserInfoBO;
import com.hlk.pojo.vo.PublisherVO;
import com.hlk.user.mapper.AppUserMapper;
import com.hlk.user.service.UserService;
import com.hlk.utils.ConvertUtils;
import com.hlk.utils.DateUtil;
import com.hlk.utils.DesensitizationUtil;
import com.hlk.utils.redis.RedisKeys;
import com.hlk.utils.redis.RedisUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @author hlk
 * @version 1.0
 * @date 2020-12-18 15:54
 */
@Service
public class UserServiceImpl implements UserService {
    private static final String USER_FACE0 = "http://122.152.205.72:88/group1/M00/00/05/CpoxxFw_8_qAIlFXAAAcIhVPdSg994.png";

    @Autowired
    private Sid sid;
    @Autowired
    private AppUserMapper appUserMapper;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public AppUser selectUserByMobile(String mobile) {
        Example example = new Example(AppUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mobile", mobile);
        AppUser user = appUserMapper.selectOneByExample(example);
        return user;
    }

    @Override
    public AppUser createUser(String mobile) {
        String userId = sid.nextShort();
        AppUser user = AppUser.builder().build()
                .setId(userId)
                .setMobile(mobile)
                .setNickname("用户：" + DesensitizationUtil.commonDisplay(mobile))
                .setBirthday(DateUtil.stringToDate("1990-01-01"))
                .setSex(Sex.secret.type)
                .setActiveStatus(UserStatusEnum.INACTIVE.type)
                .setTotalIncome(0)
                .setFace(USER_FACE0)
                .setCreatedTime(new Date())
                .setUpdatedTime(new Date());
        appUserMapper.insert(user);
        return user;
    }

    @Override
    public AppUser selectUserById(String userId) {
        return appUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public void updateUserInfo(UpdateUserInfoBO updateUserInfoBO) {
        String userId = updateUserInfoBO.getId();
        // 为保持双写一致，先删除reids，再写入数据库
        redisUtils.del(RedisKeys.getUserInfoKey(userId));

        AppUser user = ConvertUtils.sourceToTarget(updateUserInfoBO, AppUser.class);
        user.setActiveStatus(UserStatusEnum.ACTIVE.type);
        user.setUpdatedTime(new Date());

        int result = appUserMapper.updateByPrimaryKeySelective(user);
        if (result != 1) {
            throw new HuaException(ResponseStatusEnum.USER_UPDATE_ERROR);
        }

        // 缓存双删
        try {
            Thread.sleep(100);
            redisUtils.del(RedisKeys.getUserInfoKey(userId));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<PublisherVO> getUserList(List<String> userIdList) {
        List<PublisherVO> list = appUserMapper.getUserList(userIdList);
        return list;
    }

    @Override
    public AppUser getUser(String userId) {
        return appUserMapper.selectByPrimaryKey(userId);
    }
}
